package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.SendMessageRequest;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.sun.org.apache.bcel.internal.generic.NEW;

import data.Data;

public class Test {
	public static void main(String[] args) throws IOException {
		
//		DBCollection collection = Data.data("users");
//		DBObject object = collection.findAndModify(new BasicDBObject("email","chinhvd1984@gmail.com"),new BasicDBObject("$inc", new BasicDBObject("coin", -1)));
//		System.out.println(object);
//		
//		DBCursor dbObject = collection.find(new BasicDBObject("username","affsponsor").append("blogurl", "http://www.howto-gethelp.com/"));
//		while (dbObject.hasNext()) {
//			DBObject dbObject2 = (DBObject) dbObject.next();
////			dbObject2.put("paysuccess", "yes");
//			dbObject2.put("timeend", Long.parseLong(dbObject2.get("timeend").toString())-1000L*60L*60L*24L*365L);
//			collection.save(dbObject2);
//			System.out.println(new Date(Long.parseLong(dbObject2.get("timeend").toString())));
//		}
		
//		DBCollection collection = Data.data("users");
//		DBCollection coll = Data.data("blogCraw");
//		DBCursor cursor = collection.find();
//		while (cursor.hasNext()) {
//			DBObject dbObject = (DBObject) cursor.next();
//			int cout = coll.find(new BasicDBObject("username",dbObject.get("username"))).count();
//			
//			if(cout==0)
//			{
//				String email = dbObject.get("email").toString();
//				String username = dbObject.get("username").toString();
//				try {
//					String mesage = "" +
//							"ChÃ o báº¡n [ "+username+" ]<br/>" +
//							"<br/>ChÃ o má»«ng báº¡n Ä‘Ã£ Ä‘Äƒng kÃ½ thÃ nh cÃ´ng dá»‹ch vá»¥ Hatforrent cá»§a chÃºng tÃ´i.<br/>" +
//							"<br/>ChÃºng tÃ´i, nhá»¯ng ngÆ°á»�i Ä‘áº¡i diá»‡n cho há»‡ thá»‘ng Hatforrent táº¡i Viá»‡t Nam xin Ä‘Æ°á»£c Ä‘á»“ng hÃ nh cÃ¹ng sá»± phÃ¡t triá»ƒn cá»§a quÃ½ khÃ¡ch hÃ ng." +
//							"Má»�i báº¡n báº¯t Ä‘áº§u vá»›i Hatforrent theo hÆ°á»›ng dáº«n sau: <br/><a href='http://hatforrent.com/how-to-get-content'>http://hatforrent.com/how-to-get-content</a><br/><br/>" +
//							"Vá»›i má»™t sá»‘ thá»§ thuáº­t Ä‘á»ƒ láº¥y Ä‘Æ°á»£c ná»™i dung vá»›i Hatforrent:<br/> " +
//							"<a href='http://hatforrent.com/Some-tricks-to-get-content-with-Hatforrent.html'>http://hatforrent.com/Some-tricks-to-get-content-with-Hatforrent.html</a><br/></br><br/>"+
//							"Hoáº·c báº¡n thao tÃ¡c theo video sau: <br/>" +
//							"<a href='http://blog.hatforrent.com/p/video-tutorials.html'>http://blog.hatforrent.com/p/video-tutorials.html</a><br/><br/>"+
//							"Báº¡n sáº½ Ä‘Æ°á»£c dÃ¹ng thá»­ dá»‹ch vá»¥ trong 30 ngÃ y.<br/>"+
//							"Báº¡n quan tÃ¢m nhÆ°ng chÆ°a biáº¿t báº¯t Ä‘áº§u tá»« Ä‘Ã¢u hÃ£y liÃªn há»‡ vá»›i chÃºng tÃ´i. ChÃºng tÃ´i sáº½ hÆ°á»›ng dáº«n báº¡n cá»¥ thá»ƒ cÃ¡ch thá»©c sá»­ dá»¥ng dá»‹ch vá»¥.<br/><br/>" +
//							"Cáº£m Æ¡n báº¡n Ä‘Ã£ quan tÃ¢m Ä‘áº¿n dá»‹ch vá»¥ cá»§a chÃºng tÃ´i!<br/>" +
//							"<h1 style='text-align:center'>Má»�i thÃ´ng tin chi tiáº¿t xin liÃªn há»‡:</h1><br/>" +
//							"<i style='text-align:center'>PhÃ²ng há»— trá»£ khÃ¡ch hÃ ng - Hatforrent Viá»‡t Nam</i><br/></br/>" +
//							"<span style='text-align:center'>LiÃªn Thá»‹ Minh Huá»‡</span><br/>" +
//							"<span style='text-align:center'>Ä�iá»‡n thoáº¡i: (+84) 04 63278009</span><br/>" +
//							"<span style='text-align:center'>Email: support2@hatforrent.com</span><br/>" +
//							"<span style='text-align:center'>Yahoo: hatforrent_service2@yahoo.com</span><br/>" +
//							"<span style='text-align:center'>Skype: <a href='skype:hatforrent_service2?call'>hatforrent_service2</a></span><br/><br/>" +
//							"<span style='text-align:center'>LÆ°Æ¡ng Há»¯u Nam</span><br/>" +
//							"<span style='text-align:center'>Ä�iá»‡n thoáº¡i: (+84) 04 63278009</span><br/>" +
//							"<span style='text-align:center'>Email: support1@hatforrent.com</span><br/>" +
//							"<span style='text-align:center'>Yahoo: hatforrent_service1@yahoo.com</span><br/>" +
//							"<span style='text-align:center'>Skype: <a href='skype:hatforrent_service2?call'>hatforrent_service1</a></span><br/>";
//					
//					String title = "Há»— trá»£ khÃ¡ch hÃ ng sá»­ dá»¥ng dá»‹ch vá»¥ cá»§a hatforrent.com";
//		            URL url = new URL("http://yoospain.appspot.com/sendemail");
//		            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//		            connection.setDoOutput(true);
//		            connection.setRequestMethod("POST");
//		            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
//		            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
//		            writer.write("email="+email);
//		            writer.write("&name_email="+email);
//		            writer.write("&from_email=support1@hatforrent.com");
//		            writer.write("&subject="+title);
//		            writer.write("&content="+mesage);
//		            writer.close();
//		    
//		            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
//		            	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//		            }
//				} catch (Exception e) {
//					// TODO: handle exception
//					e.printStackTrace();
//				}
//				try {
//					Thread.sleep(1000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			
//			}
//		}
		
		
//		System.out.println(Utils.GetHtml("http://stackoverflow.com/search?tab=newest&q=[android]%20hasaccepted%3a1", "utf-8"));
		
		
//		DBCollection collection = Data.data("coupon");
//		
////		System.out.println(collection.find(new BasicDBObject("active","1")).count());
//////		
////		
//		for (int i = 0; i < 1; i++) {
//			
//		
//		BasicDBObject basicDBObject = new BasicDBObject();
//		basicDBObject.put("coupon", ""+i);
//		basicDBObject.put("day", 360);
//////////		
//		collection.insert(basicDBObject);
//		}
//		long time = new Date().getTime();
//		DBCursor cursor = collection.find(new BasicDBObject("timeend",new BasicDBObject("$lt",time)).append("blogurl", "http://t1u.org"));
//		System.out.println(cursor.size());
//		DBCursor cursor = collection.find(new BasicDBObject("username","phucvh.jveo"));
//		while (cursor.hasNext()) {
//			DBObject dbObject = (DBObject) cursor.next();
//			System.out.println(dbObject);
//		}
		
		DBCollection collection = Data.data("blogCraw");
		DBCollection coll = Data.data("users");
		DBCursor cursor = coll.find(new BasicDBObject("username","xuanhung2401"));
////////		
		System.out.println(cursor.count());
//////		DBCursor cursor = collection.find();
		while(cursor.hasNext())
		{
			DBObject object = cursor.next();
		
//			object.put("update", "09-11-12");
//			object.put("active_craw", "yes");
//			coll.save(object);
//			System.out.println(object);
//			long time = new Date().getTime()-30L*24L*60L*60L*1000L;
//			
//			DBCursor cursor2 = collection.find(new BasicDBObject("username",object.get("username").toString()));
//			long time = 0;
//			while (cursor2.hasNext()) {
//				DBObject dbObject = (DBObject) cursor2.next();
//				long timeend = Long.parseLong(object.get("created").toString())+30L*24L*60L*60L*1000L;
//				if(time==0)
//				{
//					time = timeend;
//				}
//				else{
//					if(time>timeend)
//						time = timeend;
//				}
//			}
//			if(time==0)
//				time=new Date().getTime();
			
//			object.put("timeend", time);
			
//			object.put("coin", 45000);
//			object.put("status", "payment");
//			coll.save(object);
//			coll.findAndModify(new BasicDBObject("blogurl","http://t1u.org"), object);
//			System.out.println(object);
		}
//		long time = new Date().getTime()+(28L*24L*60L*60L*1000L);
//		
//		long date = 28L*24L*60L*60L*1000L;
//		System.out.println(date);
//		System.out.println(new Date().getTime()+28L*24L*60L*60L*1000L);
//		System.out.println(new Date().getTime());
//		long time1 = time - new Date().getTime();
//		System.out.println(time1/(24*60*60*1000));
	}
}
